package meisong.distribute.tomcat8080.controller;

import meisong.distribute.tomcat8080.bean.User;
import meisong.distribute.tomcat8080.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author 梅松
 * @Date 2021/8/23 22:03
 */
@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping({"/","/index"})
    public String index(){
        return "index";
    }

    @RequestMapping("/toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/login")
    @ResponseBody
    public Boolean login(@RequestBody User user, HttpSession session){
        //验证成功,username放入session，跳转到首页
        if(userService.login(user)){
            session.setAttribute("loginUser",user.getUsername());
            return true;
        }else{
            return false;
        }
    }
}
